/*
 调用mui的系列函数，
 因配置上根据项目不同会有变动，所以放到不同js文件
 * */

//判断是否登陆
function IsLogin() {
	if(!config.userid)
		config.init();
	if (!config.userid) {
		dLong.alert("请先登陆！");
		jumpToLogin();
		return false;
	} else
		return true;
}

//跳转到登陆界面
function jumpToLogin() {
		var launch = plus.webview.getLaunchWebview();
		launch.show();
		mui.fire(launch, 'jumpToLoginEvent', {
			
		});
	}
//跳转到患者首页界面
function jumpToHome() {
	var launch = plus.webview.getWebviewById("huanzhe-menu");
		launch.show();
		mui.fire(launch, 'jumpTohzMenuEvent', {
		});
	}
function emptyTip(data,s){
	s=s||"";
	if(data.length<=0)
	{
	s="<img src=\"../img/empty.png\" style=\"width:100%\"/>";
	}
	return s;
}

//设置本地图片
var setImg={
	Set:function(imgId, loadUrl,defaultimg,callback){
			if (imgId == null || loadUrl == null) return;
                //图片下载成功 默认保存在本地相对路径的"_downloads"文件夹里面, 如"_downloads/logo.jpg"
                var filename = loadUrl.substring(loadUrl.lastIndexOf("/") + 1, loadUrl.length);
                var relativePath = "_downloads/" + filename;
                //检查图片是否已存在
                plus.io.resolveLocalFileSystemURL(relativePath, function(entry) {
                    console.log("图片存在,直接设置=" + relativePath);
                    //如果文件存在,则直接设置本地图片
                    setImg.setImgFromLocal(imgId, relativePath,callback);
                }, function(e) {
                    console.log("图片不存在,联网下载=" + relativePath);
                    //如果文件不存在,联网下载图片
                    setImg.setImgFromNet (imgId,loadUrl,relativePath,defaultimg,callback);
     			});
			},
    setImgFromLocal:function(imgId,relativePath,callback){
    	 //本地相对路径("_downloads/logo.jpg")转成SD卡绝对路径("/storage/emulated/0/Android/data/io.dcloud.HBuilder/.HBuilder/downloads/logo.jpg");
                var sd_path = plus.io.convertLocalFileSystemURL(relativePath);
                //给<img>设置图片
                document.getElementById(imgId).setAttribute("src", sd_path);
                callback();
    },/*联网下载图片,并设置给<img>*/
     setImgFromNet:function(imgId,loadUrl,relativePath,defaultimg,callback) {
            //先设置下载中的默认图片
            document.getElementById(imgId).setAttribute("src", "/img/"+defaultimg);
            //创建下载任务
            var dtask = plus.downloader.createDownload(loadUrl, {}, function(d, status) {
                if (status == 200) {
                    //下载成功
                    console.log("下载成功=" + relativePath);
                    setImg.setImgFromLocal(imgId, d.filename,callback);
                } else {
                    //下载失败,需删除本地临时文件,否则下次进来时会检查到图片已存在
                    console.log("下载失败=" + status+"=="+relativePath);
                    //dtask.abort();//文档描述:取消下载,删除临时文件;(但经测试临时文件没有删除,故使用delFile()方法删除);
                    if (relativePath!=null)
                        setImg.delFile(relativePath);
                }
            });
            //启动下载任务
            dtask.start();
        },
     /*删除指定文件*/
     delFile:function(relativePath) {
            plus.io.resolveLocalFileSystemURL(relativePath, function(entry) {
                entry.remove(function(entry) {
                    console.log("文件删除成功==" + relativePath);
                }, function(e) {
                    console.log("文件删除失败=" + relativePath);
                });
            });
        }
 }

function openWindow(id, url, data) {
		data = data || {};
		var aniShow = getaniShow();
		mui.openWindow({
			id: id,
			url: url,
			styles: {
				popGesture: 'close'
			},
			show: {
				aniShow: aniShow
			},
			waiting: {
				autoShow: true
			},
			extras: data
		});
	}
	//打开webview动画效果

function getaniShow() {
		var aniShow = "pop-in";
		//只有ios支持的功能需要在安卓平台隐藏；
		if (!mui.os.ios) {
			var list = document.querySelectorAll('.ios-only');
			if (list) {
				for (var i = 0; i < list.length; i++) {
					list[i].style.display = 'none';
				}
			}
			//Android平台暂时使用slide-in-right动画
				if(parseFloat(mui.os.version)<4.4){
					aniShow = "slide-in-right";
				}
		}
		return aniShow;
	}


//操作--------------原生DOM js

function hasClass(obj, cls) {
	return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(obj, cls) {
	if (!this.hasClass(obj, cls)) obj.className += " " + cls;
}

function removeClass(obj, cls) {
		if (hasClass(obj, cls)) {
			var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
			obj.className = obj.className.replace(reg, ' ');
		}
	}
	//本地存储

function setStorage(key, value) {
	//localStorage.setItem(key, value);
	plus.storage.setItem(key, value);
}

function getStorage(key) {
	//return localStorage.getItem(key);
	return plus.storage.getItem(key);
}

function removeStorage(key) {
		//localStorage.removeItem(key);
		plus.storage.removeItem(key);
	}
	//删除数组
Array.prototype.remove = function(dx) {
		if (isNaN(dx) || dx > this.length) {
			return false;
		}
		for (var i = 0, n = 0; i < this.length; i++) {
			if (this[i] != this[dx]) {
				this[n++] = this[i]
			}
		}
		this.length -= 1
	}
	//动态加载下拉框

function LoadSelect(selObj, value, text) {
		selObj.add(new Option(text, value));
	}
	//清除下拉框

function ClearSelect(selectID) {
	var obj = document.getElementById(selectID);
	obj.options.length = 0;
}

var trim = function(s) {
		return trimRight(trimLeft(s));
	}
	//去掉左边的空白 

function trimLeft(s) {
		if (s == null) {
			return "";
		}
		var whitespace = new String(" \t\n\r");
		var str = new String(s);
		if (whitespace.indexOf(str.charAt(0)) != -1) {
			var j = 0,
				i = str.length;
			while (j < i && whitespace.indexOf(str.charAt(j)) != -1) {
				j++;
			}
			str = str.substring(j, i);
		}
		return str;
	}
	//去掉右边的空白 

function trimRight(s) {
	if (s == null) return "";
	var whitespace = new String(" \t\n\r");
	var str = new String(s);
	if (whitespace.indexOf(str.charAt(str.length - 1)) != -1) {
		var i = str.length - 1;
		while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1) {
			i--;
		}
		str = str.substring(0, i + 1);
	}
	return str;
}
function getQueryStringRegExp(name,url) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(url)) return unescape(RegExp.$2.replace(/\+/g, " "));
    return "";
}
function getweek(date)
{
	var week=new Date(date).getDay();
	var str="";
	switch(week)
	{
		case 0:
		str= "星期日";
		break;
		case 1:
		str= "星期一";
		break;
		case 2:
		str= "星期二";
		break;
		case 3:
		str= "星期三";
		break;
		case 4:
		str= "星期四";
		break;
		case 5:
		str= "星期五";
		break;
		case 6:
		str= "星期六";
		break;
	}
	return str;
}
